//
//  CaogaoPreviewViewController.m
//  YiqiMobile
//
//  Created by henry on 13-6-12.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "CaogaoPreviewViewController.h"
#import "Utility.h"
#import "Config.h"
#import "JSON.h"
#import "DefineAction.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "BowenPreviewTitleCell.h"
#import "BowenPreviewCell.h"

@interface CaogaoPreviewViewController ()

@end

@implementation CaogaoPreviewViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    float height=379;
    if (iPhone5) {
        height+=88;
    }
    self.view.frame=CGRectMake(0, 0, 320, height);
    self.mytableView=[[UITableView alloc] initWithFrame:self.view.frame];
    self.mytableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    //self.mytableView.showsVerticalScrollIndicator=NO;
    self.mytableView.dataSource=self;
    self.mytableView.delegate=self;
    [self.view addSubview:self.mytableView];
    self.arrayList=[NSMutableArray array];
}

-(void) loadDataFromServer:(NSString*)caogaoId andPage:(int) page{
    [[MBProgressHUD showHUDAddedTo:self.view  animated:NO] setDetailsLabelText:LOADING_TITLE_DEFAULT];
    NSString *url=[NSString stringWithFormat:@"%@/%@",[Utility getServerUrl],LOOKBGACTION];
    NSMutableDictionary *param=[NSMutableDictionary dictionary];
    [param setObject:caogaoId forKey:@"id"];
    [param setObject:@"0" forKey:@"type"];
    [param setObject:[NSString stringWithFormat:@"%d",page] forKey:@"jr.curPageNum"];
    self.request=[[URLRequest alloc] initWithPostURL:url andDelegate:self andPostData:param andFile:nil];
    [self.request startLoadData];
}

- (void)requestFinished:(id)context{
    [MBProgressHUD hideAllHUDsForView:self.view  animated:NO];
    @try{
        NSDictionary *result=[context JSONValue];
        if (result!=nil&&[[result objectForKey:@"code"] integerValue]==0) {
            if ([[result  objectForKey:@"method"] isEqual:LOOKBGACTION]) {
                [self.arrayList removeAllObjects];
                [self.arrayList addObjectsFromArray:[result objectForKey:@"list"]];
                self.bowenDict=[NSMutableDictionary dictionaryWithDictionary:[result objectForKey:@"data"]];
                [self.mytableView reloadData];
                
            }
        }else{
            [Utility customAlertWithMsg:PROCESSERROE andTitle:ALRRTTITLE andDelagate:nil];
        }
    }@catch (NSException *e) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
    }
}

- (void)requestFailed:(id)context error:(NSString *)error{
    [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
}



//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayList count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentity2 = @"caogaoPreviewTitleCell";
        BowenPreviewCell *cell = (BowenPreviewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity2];
        if (cell ==  nil) {
            cell = [[BowenPreviewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity2];
        }
    NSDictionary *dict=[self.arrayList objectAtIndex:indexPath.row];
    cell.contentText.hidden=YES;
    cell.eMAsyncImageView.hidden=YES;
    cell.voiceButton.hidden=YES;

    switch ([[dict objectForKey:@"count1"] integerValue]) {
        case 1:{//文本
            cell.contentText.hidden=NO;
            UIFont *font=[UIFont fontWithName:FONT_FAMILY size:12.0f];
            CGSize size=[Utility getStringSize:[dict objectForKey:@"str5"] font:font withMaxSize:CGSizeMake(230, 100)];
            if (size.height>=60) {
                cell.contentText.numberOfLines=3;
                [cell setFrame:CGRectMake(0, 0, 320, 100)];
            }else{
                cell.contentText.numberOfLines=(size.height/20);
                [cell setFrame:CGRectMake(0, 0, 320, size.height+40)];
            }
            
            if (size.height>=60) {
                cell.contentText.frame=CGRectMake(66, 36, 230, 60);
            }else{
                cell.contentText.frame=CGRectMake(66, 36, 230, size.height);
            }
            cell.contentText.text=[dict objectForKey:@"str5"];
            break;
        }
        case 2:{//图片
            cell.eMAsyncImageView.hidden=NO;
            cell.eMAsyncImageView.imageUrl=[NSString stringWithFormat:@"%@/%@?id=%@&type=1",[Utility  getServerUrl],DOWNFILEACTION,[dict objectForKey:@"str1"]];
            cell.eMAsyncImageView.frame=CGRectMake(66,36,100,100);
            cell.eMAsyncImageView.imageId=[dict objectForKey:@"str1"];
            
            [cell setFrame:CGRectMake(0, 0, 320, cell.eMAsyncImageView.frame.size.height+40)];
            break;
        }
        case 3:{//语音
            cell.voiceButton.hidden=NO;
            cell.voiceButton.frame=CGRectMake(66, 36, 38, 38);
            [cell.voiceButton setImage:[UIImage imageNamed:@"yuyinButton.png"] forState:UIControlStateNormal];
            [cell.voiceButton setImage:[UIImage imageNamed:@"yuyinButton.png"] forState:UIControlStateSelected];
            [cell.voiceButton setImage:[UIImage imageNamed:@"yuyinButton.png"] forState:UIControlStateHighlighted];
            cell.voiceButton.tag=indexPath.row;
            [cell.voiceButton addTarget:self action:@selector(playmp3Action:) forControlEvents:UIControlEventTouchUpInside];
            [cell setFrame:CGRectMake(0, 0, 320, cell.voiceButton.frame.size.height+40)];
            break;
        }
        case 4:{//视频
            
            break;
        }
    }
    cell.nickName.text=[dict objectForKey:@"str3"];
    cell.logo.imageUrl=[NSString stringWithFormat:@"%@/%@?id=%@&type=3",[Utility  getServerUrl],DOWNFILEACTION,[dict objectForKey:@"str4"]];

    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)playmp3Action:(UIButton*)sender{
    voiceLoading=[[UIView alloc] initWithFrame:sender.frame];
    voiceLoading.backgroundColor=[UIColor colorWithRed:249.0/255 green:249.0/255 blue:249.0/255 alpha:1.0];
    UIActivityIndicatorView *activityIndicatorView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicatorView.center=CGPointMake((sender.frame.size.width-20)/2, (sender.frame.size.height-30)/2);
    [activityIndicatorView startAnimating];
    [voiceLoading addSubview:activityIndicatorView];
    [sender  addSubview:voiceLoading];
    
    NSDictionary *dict=[self.arrayList objectAtIndex:[sender tag]];
    NSString *url=[NSString stringWithFormat:@"%@/%@?id=%@&type=2",[Utility  getServerUrl],DOWNFILEACTION,[dict objectForKey:@"str1"]];
    EMAsyncFile *eMAsyncFile=[[EMAsyncFile alloc] init];
    eMAsyncFile.imageIdKey=@"id";
    eMAsyncFile.delegate=self;
    [eMAsyncFile setImageUrl:url];
    
}
#pragma -mark EMAsyncFileDelegate
-(void) fileSuccess:(NSString*)url{
    [voiceLoading removeFromSuperview];
    self.voiceRecord=[[VoiceRecord alloc] init];
    self.voiceRecord.delegate=self;
    [self.voiceRecord playMp3Click:url];
    
}
-(void) fileFail:(NSString*)error{
    [voiceLoading removeFromSuperview];
    
}




@end
